package com.gzy.xuexiziliao.controller;

import com.gzy.xuexiziliao.common.result.PageResult;
import com.gzy.xuexiziliao.common.result.Result;
import com.gzy.xuexiziliao.pojo.entity.Categories;
import com.gzy.xuexiziliao.service.InformationCategoriesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @Description: 分类信息
 * @Author: george
 * @Create: 2023/4/16 10:05
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/admin/categories")
public class InformationCategoriesController {

    @Autowired
    private InformationCategoriesService informationCategoriesService;
    @PostMapping
    public Result save(@RequestBody Categories categories){
        log.info("新增分类,categories:{}", categories);
        informationCategoriesService.save(categories);
        return Result.success("添加成功");
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除分类,categories:{}", ids);
        informationCategoriesService.delete(ids);
        return Result.success("删除成功");
    }

    @PutMapping
    public Result update(@RequestBody Categories categories){
        log.info("修改分类,categories:{}", categories);
        informationCategoriesService.update(categories);
        return Result.success("修改成功");
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询分类,id:{}", id);
        Categories categories = informationCategoriesService.getById(id);
        return Result.success(categories);
    }

    @GetMapping("/list")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
        log.info("分页查询参数：{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        PageResult pageResult=informationCategoriesService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageResult);
    }

    //分类的启用与禁用
    @PostMapping("/status/{status}")
    public Result status(@PathVariable Integer status,Integer id){
        log.info("修改分类状态,id:{},status:{}",id,status);
        informationCategoriesService.startOrCategories(status,id);
        return Result.success("分类启用成功");
    }



}
